package com.example.yyy

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.*
import kotlinx.android.synthetic.main.activity_recycler.*
import org.jetbrains.anko.toast


class RecyclerActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recycler)
        var mutableList :MutableList<Planet> = mutableListOf()
        var planet = Planet(R.drawable.ic_launcher_background, "水星")
        mutableList.add(planet)
        mutableList.add(Planet(R.drawable.tp, "水星"))
        mutableList.add(planet)
        mutableList.add(planet)
        //ry_re_recycler.layoutManager = LinearLayoutManager(this)
        //ry_re_recycler.layoutManager =GridLayoutManager(this,2)
        ry_re_recycler.layoutManager =StaggeredGridLayoutManager(2,DividerItemDecoration.VERTICAL)
        val adapterR = RecyclerLinearAdapter(this, mutableList)
        adapterR.setListener(adapterR)
        ry_re_recycler.adapter = adapterR;
        ry_re_recycler.addItemDecoration(DividerItemDecoration(this,DividerItemDecoration.HORIZONTAL))
    }


    class RecyclerLinearAdapter(private val context: Context,private val infos:MutableList<Planet>):
        RecyclerView.Adapter<RecyclerView.ViewHolder>(),OnItemListener, View.OnClickListener,
        View.OnLongClickListener {

        val inflater:LayoutInflater = LayoutInflater.from(context)

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            val view:View? = inflater.inflate(R.layout.list_layout,parent,false)
            view?.setOnClickListener(this)
            view?.setOnLongClickListener(this)
            return ItemHolder(view)
        }

        private class ItemHolder(itemView: View?): RecyclerView.ViewHolder(itemView!!) {
            var im: ImageView = itemView!!.findViewById(R.id.iv_lt_planet)
            var tv: TextView = itemView!!.findViewById(R.id.tv_lt_planet)
        }


        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            val planet = infos[position]
            var vh: ItemHolder = holder as ItemHolder
            vh.itemView.tag=position
            vh.im.setImageResource(planet.image)
            vh.tv.text = planet.str
        }
        //获取列表项数目
        override fun getItemCount(): Int = infos.size

        private var itemListener: OnItemListener? =null
        fun setListener(listener: OnItemListener){
            this.itemListener = listener
        }

        override fun onClick(v: View?) {
            itemListener?.onItemClick(v, v?.tag as Int)
        }

        override fun onLongClick(v: View?): Boolean {
            itemListener?.onItemLongClick(v, v!!.tag as Int)
            return true
        }

        override fun onItemClick(view: View?, position: Int) {

            context.toast("你点击了${infos[position] }")
        }

        override fun onItemLongClick(view: View?, position: Int) {
            context.toast("你长按了${infos[position]}")
        }


    }
}

